package com.witmob.rivers;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 列表显示Activity，也是应用的入口Activity
 * @author marshal
 *
 */
public class ListViewActivity extends FragmentActivity {

	private ListView riverListView;

	private CursorAdapter adapter;
	
	listViewAdapter viewAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

//		initLoader();
		initRiverListView();
	}

	public void onClick(View view) {
		Log.d("list", "click!");
	}

	/**
	 * 初始化Loader，为ListView数据做异步加载，即不依赖UI线程加载
	 */
	private void initLoader() {
		getSupportLoaderManager().initLoader(0, null,
				new LoaderCallbacks<Cursor>() {

					@Override
					public Loader<Cursor> onCreateLoader(int id, Bundle args) {
						Log.d("list", "on create loader");
						CursorLoader cursorLoader = new CursorLoader(
								ListViewActivity.this,
								RiverContentProvider.CONTENT_URI, new String[] {
										RiverContentProvider._ID,
										RiverContentProvider.NAME,
										RiverContentProvider.INTRODUCTION,
										RiverContentProvider.IMAGE_URL },
								null, null, null);
						return cursorLoader;
					}

					@Override
					public void onLoadFinished(Loader<Cursor> loader,
							Cursor cursor) {
						Log.d("list", "on loader finished");
						adapter.swapCursor(cursor);
					}

					@Override
					public void onLoaderReset(Loader<Cursor> loader) {
						Log.d("list", "on loader reset");
						adapter.swapCursor(null);
					}
				});
	}

	/**
	 * 初始化RiverListView，主要时设置Cursor和adapter，以及点击ListView条目的动作
	 */
	private void initRiverListView() {
		riverListView = (ListView) this.findViewById(R.id.riverList);

//		Cursor cursor = managedQuery(RiverContentProvider.CONTENT_URI, null,
//				null, null, null);
//		adapter = new SimpleCursorAdapter(this, R.layout.row, cursor,
//				new String[] { RiverContentProvider.NAME,
//						RiverContentProvider.INTRODUCTION }, new int[] {
//						R.id.riverName, R.id.riverIntroduction},
//				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//
//		riverListView.setAdapter(adapter);
//
		viewAdapter = new listViewAdapter(this);
		riverListView.setAdapter(viewAdapter);
		viewAdapter.refresh();
		riverListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				River river = (River) view.getTag();
				Log.d("list", ">>" + river.getId());
				Intent intent = new Intent();
				intent.setClass(ListViewActivity.this, DetailViewActivity.class);
				intent.putExtra(GlobalValues.RIVER_ID, river.getId());
				startActivity(intent);
			}
		});
		
	}
	
	private class listViewAdapter extends BaseAdapter{

		private List<River> riverList = new ArrayList<River>();
		
		private ListViewActivity context;
		
		public listViewAdapter(Context context){
			this.context = (ListViewActivity) context;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return riverList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return riverList.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			River river = riverList.get(position);
			if(convertView == null){
				convertView = context.getLayoutInflater().inflate(R.layout.row, parent, false);
			}
			((TextView) convertView.findViewById(R.id.riverName)).setText(river.getName());
			((TextView) convertView.findViewById(R.id.riverIntroduction)).setText(river.getIntroduction());
			final ImageView imageView = (ImageView) convertView.findViewById(R.id.river_image);
			AsyncImageLoader imageLoader = new AsyncImageLoader();
			Drawable drawable0;
			drawable0 = imageLoader.loadDrawable(river.getImageUrl(),
					new AsyncImageLoader.ImageCallback() {
						public void imageLoaded(final Drawable imageDrawable,
								final String imageUrl) {
							imageView.refreshDrawableState();		
							imageView.setImageDrawable(imageDrawable);
						}
					});
			imageView.setImageDrawable(drawable0);
			convertView.setTag(river);
			return convertView;
		}
		
		public void getReault(){
			
			Cursor cursor = managedQuery(RiverContentProvider.CONTENT_URI, null,
					null, null, null);
			while (cursor.moveToNext()) {
				River river = new River();
				river.setId(cursor.getInt(0));
				river.setName(cursor.getString(1));
				river.setLength(cursor.getInt(2));
				river.setIntroduction(cursor.getString(3));
				river.setImageUrl(cursor.getString(4));
				riverList.add(river);
			}
			notifyDataSetChanged();
		}
		
		public void refresh(){
			riverList.clear();
			getReault();
		}
	}
}